#include <iostream>
#include <fstream>
#include <string>
#include <mutex>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"
namespace ns_util
{
    class FileUtil
    {
    public:
        /*如果去掉 static，ReadFile 就是非静态成员函数，它内部隐含了一个指向对象的 this 指针，调用时得先有对象*/
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            std::ifstream in(file_path, std::ios::in); // 这里的in是创建了一个流对象
            if (!in.is_open())
            {
                std::cerr << "file open cerr" << std::endl;
                return false;
            }

            std::string line;
            while (std::getline(in, line))
            {
                // out->append(line); -> 操作符，它的作用是：先解引用指针，再访问成员
                (*out).append(line);
            }
            in.close();

            return true;
        }
    };

    class StringUtile
    {
    public:
        static void CutString(const std::string &line, std::vector<std::string> *res, const std::string &sep)
        {
            boost::split(*res, line, boost::is_any_of(sep), boost::token_compress_on);
        }
    };

    // 这里是是jieba库的作用
    #include "cppjieba/Jieba.hpp"
    const char *const DICT_PATH = "./dict/jieba.dict.utf8";
    const char *const HMM_PATH = "./dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char *const IDF_PATH = "./dict/idf.utf8";
    const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";

    class JiebaUtil
    {
    private:
        // static cppjieba::Jieba JIEBA;
        cppjieba::Jieba jieba;
        JiebaUtil():jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH){}
        JiebaUtil(const JiebaUtil&)=delete;//单例模式不能有拷贝构造

        //一个单例模式的指针
        static JiebaUtil *instance;

    public:
        
        static JiebaUtil* get_instance()
        {
            static std::mutex Lock;
            if(nullptr==instance)
            {
                Lock.lock();
                instance=new JiebaUtil();
                // instance->InitJiebaUtil();
                Lock.unlock();
            }

            return instance;
        }

        void InitJiebaUtil()
        {
            //去掉暂停词
        }
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            get_instance()->jieba.CutForSearch(src, *out);
        }
    };
    JiebaUtil *JiebaUtil::instance=nullptr;
};